const jwt = require('jsonwebtoken')

const generateTokens=(payload)=>{
    const accessToken=jwt.sign(
        payload,
        process.env.JWT_ACCESS_SECRET,
        {expiresIn:'15m'}
    )
    const refreshToken=jwt.sign(
        {...payload,tokenType:'refresh'},
        process.env.JWT_REFRESH_SECRET,
        {expiresIn:'7d'}
    )
    return {accessToken,refreshToken}
}

const verifyToken=(token,isRefresh=false)=>{
    const secret=isRefresh?
        process.env.JWT_REFRESH_SECRET:
        process.env.JWT_ACCESS_SECRET
    return jwt.verify(token,secret)
}

module.exports={generateTokens,verifyToken}
